package com.woniuxy.service.impl;

import com.woniuxy.dao.MenuDao;
import com.woniuxy.pojo.Menu;
import com.woniuxy.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
    @Override
    public List<Menu> getAllMenus() {
        return menuDao.getAllMenus();
    }

    @Override
    public Set<String> getUrlByUserName(String userName) {
        return this.menuDao.getUrlByUserName(userName);
    }

    @Override
    public boolean assignPermission(Integer roleId, String menus) {
        boolean flag = false;
        try {
            this.menuDao.delete(roleId);
            String[] menusArr = menus.split(",");
            if (menusArr!=null&&menusArr.length>0){
                for(int i=0;i<menusArr.length;i++){
                    Map<String,Integer> map = new HashMap<String,Integer>(2);
                    map.put("roleId",roleId);
                    map.put("menuId",Integer.parseInt(menusArr[i]));
                    this.menuDao.save(map);
                }
            }
            flag = true;
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        return flag;
    }
}
